var app = require("app");
cc.Class({
	extends: require("BaseForm"),

	properties: {

	},

	OnCreateInit: function () {
		this.ZorderLv = 8;
		this.HeroAccountManager = app.HeroAccountManager();
		this.ShareDefine = app.ShareDefine();

		this.RegEvent("CodeError", this.Event_CodeError);
		this.RegEvent("ConnectFail", this.Event_ConnectFail);

		this.isToken = 0;
		this.tokenAccount = 0;

		this.stringArr = [];

		this.loginNode = this.node.getChildByName("login");
		this.signNode = this.node.getChildByName("sign")

		this.loginAccount = this.loginNode.getChildByName("layout").getChildByName("account").getChildByName("bg").getChildByName("EditBoxName").getComponent(cc.EditBox);
		this.loginPass = this.loginNode.getChildByName("layout").getChildByName("password").getChildByName("bg").getChildByName("EditBoxName").getComponent(cc.EditBox);

		this.signAccount = this.signNode.getChildByName("layout").getChildByName("account").getChildByName("bg").getChildByName("EditBoxName").getComponent(cc.EditBox);
		this.signPass = this.signNode.getChildByName("layout").getChildByName("password").getChildByName("bg").getChildByName("EditBoxName").getComponent(cc.EditBox);
		this.checkPass = this.signNode.getChildByName("layout").getChildByName("checkpsd").getChildByName("bg").getChildByName("EditBoxName").getComponent(cc.EditBox);

		this.loginNodeArr = [this.loginAccount, this.loginPass];
		this.signNodeArr = [this.signAccount, this.signPass, this.checkPass];
	},

	//登录错误码
	Event_CodeError: function (event) {
		let argDict = event;
		let code = argDict["Code"];
		if (code == this.ShareDefine.KickOut_AccountTokenError) {
			this.EditBoxPsw.string = "";
		}
		else if (code == this.ShareDefine.KickOut_ServerClose) {
			this.WaitForConfirm("Code_10016", [], [], this.ShareDefine.ConfirmYN)
		}
		else if (code == this.ShareDefine.Http_PackNotAction) {
			//this.WaitForConfirm("Code_10012", [], [], this.ShareDefine.ConfirmYN)
			this.ShowSysMsg("Account_AccountInputError");
			
		}
	},

	//连接服务器失败
	Event_ConnectFail: function (event) {
		this.UpdateAccessPoint();
		let argDict = event;
		if (!argDict['bCloseByLogout'])
			this.ShowSysMsg("Net_ConnectFail");
	},

	UpdateAccessPoint: function () {
		app.HeroAccountManager().UpdateAccessPoint();
		let clientConfig = app.Client.GetClientConfig();
		let gameServerIP = clientConfig["GameServerIP"];
		let gameServerPort = clientConfig["GameServerPort"];
		//换节点
		this.url = ['ws://', gameServerIP, ':', gameServerPort].join("");
	},

	OnShow: function () {
		//每次打开显示账号
		this.loginNode.active = true;
		this.signNode.active = false;
		this.ShowAccountInfo();
	},

	changeBox() {
		this.loginNode.active = !this.loginNode.active;
		this.signNode.active = !this.signNode.active;
	},

	OnclickAll(eventTouch) {
		let btnNode = eventTouch.currentTarget;
		let btnName = btnNode.name;
		switch (btnName) {
			case "btn_close":
				this.CloseForm();
				break;
			case "btn_zhzc":
				this.changeBox();
				break;
			case "btn_fhdl":
				this.changeBox();
				break;
			case "btn_login":
				this.Click_btnLogin();
				break;
			case "btn_sign":
				this.Click_btnSign();
				break;
		}
	},

	//点击注册
	Click_btnSign: function () {
		let acc = this.signNodeArr[0].string;
		let psd = this.signNodeArr[1].string;
		let cpsd = this.signNodeArr[2].string;

		if (psd !== cpsd) {
			this.ShowSysMsg("Account_PswNotSame");
			return
		}
		//注册
		this.HeroAccountManager.SendRegAccount(acc, psd, cpsd);
	},

	//点击登陆
	Click_btnLogin: function () {
		let charAccount = this.loginNodeArr[0].string;
		if (!charAccount) {
			this.ShowSysMsg("AccountLogin_NotInputAccount");
			return
		}
		let psw = this.loginNodeArr[1].string;;
		if (!psw) {
			this.ShowSysMsg("AccountLogin_NotInputPsw");
			return
		}

		//如果密码等于token为token登录
		if (this.isToken && this.tokenAccount == charAccount) {
			psw = this.HeroAccountManager.GetAccountToken(charAccount);
		} else {
			this.isToken = 0;
		}

		//账号密码登陆
		this.HeroAccountManager.AccountLogin(charAccount, psw, this.isToken);
	},

	//显示账号信息
	ShowAccountInfo: function () {
		let accountList = this.HeroAccountManager.GetLocalAccountList();
		let count = accountList.length;

		let account = "";
		let token = "";
		//有登陆记录
		if (count) {
			account = accountList[count - 1];
			token = this.HeroAccountManager.GetAccountToken(account);
			if (token) {
				this.isToken = 1;
				this.tokenAccount = account;
			}
			this.loginNodeArr[0].string = account;
			this.loginNodeArr[1].string = '******';
		}
	},
});
